import json
import pytest
import requests

from utils.read_yaml import read_yaml

class  Send:

    #通用方法
    def _perform_request(self, session, request_data):
        method = request_data.get("method", "get").lower()
        url = request_data.get("url")
        params = request_data.get("param",{})

        if method == "get":
            # params = request_data.get("param", {})
            #请求并获取响应对象
            resp = session.get(url=url, params=params)
        elif method == "post":
            # params = request_data.get("param", {})
            resp = session.post(url=url, data=params)
        elif method == "put":
            # params = request_data.get("param", {})
            resp = session.put(url=url,data=params)
        elif method == "delete":
            resp = session.delete(url=url)
        else:
            raise ValueError(f"Unsupported HTTP method: {method}")

        #添加if判断返回 json? HTML页面?

        resp.encoding = "utf-8"
        #####读取http状态码
        code = resp.status_code
        #resp.text为str格式，将字符串转换为字典格式
        body = resp.text

        content_type = resp.headers.get("Content-Type", "").lower()

        if "application/json" in content_type:
            body = json.loads(body)

            print(f"Request: {method.upper()} {url}")
            print(f"Response: {code}, {body}")

            return {"code": code, "body": body}
        elif "text/html" in content_type:
            body = body
            return body
        else:
            return

        # body = json.loads(body)
        #
        # print(f"Request: {method.upper()} {url}")
        # print(f"Response: {code}, {body}")
        #
        # return {"code":code, "body":body}

    # 实现不同用户登录
    def ceshi_buy_login(self, username):
        buy_login = read_yaml("D:/Projects/接口自动化/data/ceshi_buy_login.yaml")

        login_data = buy_login[username]
        session = requests.Session()
        self._perform_request(session, login_data)
        return session

    #登录
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/login.yaml"))
    def send_login(self, session, args):
        return self._perform_request(session, args)

    #注册
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/register.yaml"))
    def send_register(self, session, args):
        #调用通用方法
        return self._perform_request(session, args)

    #添加银行卡
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/bankcard_add.yaml"))
    def send_bankcard_add(self, session, args):
        return self._perform_request(session, args)

    #删除银行卡
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/bankcard_delete.yaml"))
    def send_bankcard_delete(self, session, args):
        return self._perform_request(session, args)

    #修改银行卡
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/bankcard_update.yaml"))
    def send_bankcard_update(self, session, args):
        return self._perform_request(session, args)

    #买入零钱理财
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/buy_change.yaml"))
    def send_buy_change(self, session, args):
        return self._perform_request(session, args)

    #买入期限理财
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/buy_termFinancal.yaml"))
    def send_buy_termFinancal(self, session, args):
        return self._perform_request(session, args)

    #买入基金理财
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/buy_fundProduct.yaml"))
    def send_buy_fundProduct(self, session, args):
        return self._perform_request(session, args)

    #申请贷款
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/loan.yaml"))
    def send_loan(self, session, args):
        return self._perform_request(session, args)

    #接口越权测试
    @pytest.mark.parametrize("args", read_yaml("D:/Projects/接口自动化/data/unauthorized.yaml"))
    def send_unauthorized(self, session, args):
        return self._perform_request(session, args)